<?php

defined('__MAPEXEC__') or die('No direct access allowed');

class Maps_Geocode_Cache {
  
  private $coords = array();
  private static $me = null;
  
  /**
   * Detects if an address exists in the cache.
   * @param   String    Fully qualified street address
   * @return  Boolean   True if the address exists in the cache.
   */
  private function exists($address) {
    return !empty($this->coords[$address]);
  }
  
  /**
   * Flushes the cache.
   * @param   void
   * @return  void
   */
  public function flush() {
    $this->coords = array();
  }
  
  /**
   * Gets the latitude and longitude for a cached address
   * @param   string  Fully qualified street address
   * @return  array   Array with lat and lng points or empty array if not found.
   */
  public function get($address) {
    if ( $this->exists($address) ) {
      return new Maps_LatLng($this->coords[$address]['lat'], $this->coords[$address]['lng']);
    }
    return false;
  }
  
  /**
   * Inserts an address and geo coordinates into the cache.
   * @param   string  Fully qualified street address
   * @param   double  Degrees latitude
   * @param   double  Degrees longitude
   * @return  void
   */
  public function insert($address, Maps_LatLng $latlng) {
    $this->coords[$address] = $latlng->toArray();
  }
  
  /**
   * Gets the only instance of this class.
   * @param   void
   * @return  Maps_Geo_Cache  The cache object
   */
  public static function instance() {
    if ( !isset(self::$me) ) {
      self::$me = new self();
    }
    return self::$me;
  }
  
}

?>